﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace giaodien
{
    public partial class XemLuotThemHang : Form
    {
        public XemLuotThemHang()
        {
            InitializeComponent();
        }
        ConnectData obj=new ConnectData();
        DataTable dt = new DataTable();
        DataTable dtNam = new DataTable();
        int iCachLoadListView = 0;
        Thread _TatForm;
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            UI.MouseDown(e);
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            UI.MouseMove(this, MousePosition.X, MousePosition.Y);
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            UI.MouseUp();
        }
        private void XemLuotThemHang_Load(object sender, EventArgs e)
        {
            _TatForm = new Thread(new ThreadStart(TatForm));
            dt = obj.ThemSanPham();
            dtNam = obj.laydulieuBang("select distinct ngaythem from themsanpham");
            object[] Nam=new object[0];
            object[] Tam;
            int j = 0;
            for (int i=0;i<dtNam.Rows.Count;i++) {
                if (Nam.Contains(DateTime.Parse(dtNam.Rows[i][0].ToString()).Year) == false)
                {
                    
                    Tam = Nam;
                    Nam=ChuyenMang(Nam,Tam);
                    Nam[j]=DateTime.Parse(dtNam.Rows[i][0].ToString()).Year;
                    j++;
                }
            }
            cmbNam.Items.AddRange(Nam);
            chkNam.Checked = true;
            cmbNam.SelectedIndex = 0;
            cmbThang.SelectedIndex = 0;  
        }

        private void TatForm()
        {
            Application.Run(new QLNhapHang());
        }
        //giong nhu lam bien tam
        private object[] ChuyenMang(object[] Nam, object[] Tam)
        {
            Nam = new object[Tam.Length + 1];
            for (int i = 0; i < Tam.Length; i++) { 
                Nam[i]=Tam[i];
            }
            return Nam;
        }
        //xet dieu kienj chay tung
        void LoadListView(int thang, int nam)
        {
            switch (iCachLoadListView) {
                case 1: {
                    LoadListViewTheoThangVaNam(thang,nam);
                    break;
                }
                case 2: {
                    LoadListViewTheoNam(nam);
                    break;
                }
                case 3: {
                    LoadListViewTheoThang(thang);
                    break;
                }
            }
        }

        private void LoadListViewTheoThang(int thang)
        {
            string query = "select * from themsanpham where ngaythem like '%/"+thang+"/%'";
            dt = obj.laydulieuBang(query);
            hienthilistview(dt);
        }

        private void LoadListViewTheoNam(int nam)
        {
            string query = "select * from themsanpham where ngaythem like '%/" + nam + "'";
            dt = obj.laydulieuBang(query);
            hienthilistview(dt);
        }

        private void LoadListViewTheoThangVaNam(int thang, int nam)
        {
            string query = "select * from themsanpham where ngaythem like '%" + thang + "/" + nam + "'";
            dt = obj.laydulieuBang(query);
            hienthilistview(dt);
        }

        void hienthilistview(DataTable dt)
        {
            lvwXemLuotThemHang.Items.Clear();
            foreach (DataRow dr in dt.Rows)
            {
                ListViewItem it = new ListViewItem();
                it.Text = dr[0].ToString();
                it.SubItems.Add(dr[1].ToString());
                it.SubItems.Add(dr[2].ToString());
                it.SubItems.Add(dr[3].ToString());
                it.SubItems.Add(dr[4].ToString());
                lvwXemLuotThemHang.Items.Add(it);
            }
        }


        void ThemItemVaoListView(DataRow dr)
        {
            ListViewItem it = new ListViewItem();
            it.Text = dr[0].ToString();
            it.SubItems.Add(dr[1].ToString());
            it.SubItems.Add(dr[2].ToString());
            it.SubItems.Add(dr[3].ToString());
            it.SubItems.Add(dr[4].ToString());
            lvwXemLuotThemHang.Items.Add(it);
        }

        private void chkThang_CheckedChanged(object sender, EventArgs e)
        {
            if (cmbNam.SelectedItem == null || cmbThang.SelectedItem == null)
                return;

            ChonCachLoadListView();
            LoadListView(int.Parse(cmbThang.SelectedItem.ToString()), int.Parse(cmbNam.SelectedItem.ToString()));
        }


        private void ChonCachLoadListView() {
            if (chkNam.Checked == true && chkThang.Checked == true)
            {
                iCachLoadListView = 1;
            }
            else
            {
                if (chkNam.Checked == true && chkThang.Checked == false)
                {
                    iCachLoadListView = 2;
                }
                else
                {
                    if (chkNam.Checked == false && chkThang.Checked == true)
                    {
                        iCachLoadListView = 3;
                    }
                }
            }
        }
        private void cmbThang_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbNam.SelectedItem == null || cmbThang.SelectedItem == null)
                return;

            ChonCachLoadListView();
            LoadListView(int.Parse(cmbThang.SelectedItem.ToString()), int.Parse(cmbNam.SelectedItem.ToString()));
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
            _TatForm.Start();
        }
    }
}
